﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Media.Imaging;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;

namespace MOJsdk
{
    public class MojToolButton : Button, IComparable
    {
        public string Letter { get; set; }
        public string Image { get; set; }
        public string ToolTipText { get; set; }
        public string UniqueID { get; set; }

        public MojToolButton(string letter, string uniqueID, string tooltiptext, string imagePath)
        {
            _showTypes = new List<string>();
            Letter = letter;
            UniqueID = uniqueID;

            if (!Letter.IsNullorEmpty())
                this.Content = Letter;

            if (tooltiptext != null)
                ToolTipText = tooltiptext;
            else
                ToolTipText = "";
            ToolTip = ToolTipText;

            if (System.IO.File.Exists(imagePath))
                Image = imagePath;
            else
                Image = null;
        }

        private List<string> _showTypes;
        public List<string> ShowConditions
        { get { return _showTypes; } }

        #region AddCondition
        public void AddShowCondition(string condition)
        {
            condition = condition.ToLower();
            if(!_showTypes.Contains(condition))
                _showTypes.Add(condition);
        }
        public void AddShowCondition(MojToolButtonShowConditions condition)
        {
            string str = condition.ToString().ToLower();
            AddShowCondition(str);
        }
        public void AddShowCondition(string condition1, string condition2)
        {
            AddShowCondition(condition1.ToLower() + ";" + condition2.ToLower());
        }
        public void AddShowCondition(MojToolButtonShowConditions condition1, string condition2)
        {
            AddShowCondition(condition1.ToString(), condition2);
        }
        public void AddShowCondition(MojToolButtonShowConditions condition1, MojToolButtonShowConditions condition2)
        {
            AddShowCondition(condition1.ToString(), condition2.ToString());
        }
        #endregion

        #region Check
        public void SetVisibility(Visibility VisibilityType)
        {
            this.Visibility = VisibilityType;
        }
        public void CheckVisibility()
        {
            if (_showTypes.Count == 0)
                return;

            if (CheckOrCondition(_showTypes))
                this.Visibility = System.Windows.Visibility.Visible;
            else
                this.Visibility = System.Windows.Visibility.Hidden;
        }
        private bool CheckCondition(string condition) 
        {
            switch (condition)
            {
                case "allways":
                    return true;

                case "movieisselected":
                    return GlobalVars.TheHost.sv_IsMovieSelect;

                case "fileisselected":
                    return GlobalVars.TheHost.sv_IsFileSelected;

                case "panelimportmovies":
                    return GlobalVars.TheHost.sv_DisplayPanel.ToLower() == "panelimportmovies";

                case "paneldisplaymovies":
                    return GlobalVars.TheHost.sv_DisplayPanel.ToLower() == "paneldisplaymovies";

                default:
                    return true;
            }
        }
        private bool CheckOrCondition(IList<string> conditions)
        {
            List<bool> _list = new List<bool>();

            foreach (var str in conditions)
            {
                if (str.ToList(";").Count > 1)
                    _list.Add(CheckAndCondition(str.ToList(";")));
                else
                    _list.Add(CheckCondition(str));
            }

            return _list.Contains(true);
        }
        private bool CheckAndCondition(IList<string> conditions)
        {
            List<bool> _list = new List<bool>();

            foreach (var str in conditions)
            {
                if (str.ToList(";").Count > 1)
                    _list.Add(CheckAndCondition(str.ToList(";")));
                else
                    _list.Add(CheckCondition(str));
            }

            return !_list.Contains(false);
        }
        #endregion

        public int CompareTo(object obj)
        {
            MojToolButton btn = obj as MojToolButton;
            return UniqueID.CompareTo(btn.UniqueID);
        }
        public new bool Equals(object obj)
        {
            MojToolButton btn = obj as MojToolButton;
            return UniqueID == btn.UniqueID;
        }

    }
    public class MojToolButtonCollection
    {
        private ObservableCollection<MojToolButton> _buttons;

        public MojToolButtonCollection()
        {
            _buttons = new ObservableCollection<MojToolButton>();
        }

        public void Add(MojToolButton ToolBtn)
        {
            if (_buttons.Contains(ToolBtn))
                return;

            ToolBtn.Width = 20;
            ToolBtn.Height = 20;
            ToolBtn.ApplyStyle(ToolBtn.UniqueID);

            _buttons.Add(ToolBtn);
            Update();
        }
        public void Remove(MojToolButton ToolBtn)
        {
            if (_buttons.Contains(ToolBtn))
            {
                _buttons.Remove(ToolBtn);
                Update();
            }
        }

        public void Update()
        {
            GlobalVars.TheHost.AddLogEntry("Starting updating Toolbuttons Collection");
            UpdateVisibility();
            Add2StackPanel();
            GlobalVars.TheHost.AddLogEntry("ToolbuttonCollection updated sucessfully");
        }
        private void UpdateVisibility()
        {
            foreach (var b in _buttons)
                b.CheckVisibility();
        }
        private void Add2StackPanel()
        {
            GlobalVars.TheHost.TopToolBar.Children.Clear();
            foreach (var v in _buttons)
                GlobalVars.TheHost.TopToolBar.Children.Add(v);
        }
    }
    public enum MojToolButtonShowConditions
    {
        Allways,
        MovieIsSelected,
        FileIsSelected,
        PanelImportMovies,
        PanelDisplayMovies,
    }
}



























